﻿using DotNetDetour;
using Mcl.Core.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using WPFLauncher.Manager;
using WPFLauncher.Model.App;
using WPFLauncher.Model.Chat;
using WPFLauncher.Util;
using WPFLauncher.View.Launcher;
using WPFLauncher.View.UI;

namespace KFX.DotNetDetour.Main
{
    internal class initiate : IMethodHook
    {
        [HookMethod("WPFLauncher.Util.uy")]
        public static aov a(string gjo, string gjp, EventHandler gjq, aot gjr, string gjs = null, bool gjt = false, Action<string> gju = null)
        {
            Socket.send("Game_Start" + gjp);
            CheckStart_FILE();
            string gjp2 = File.ReadAllText(FilePath);
            gameStartManager = a_Original(gjo, gjp2, gjq, gjr, gjs, gjt, gju);

            new Thread(delegate ()
            {
                while (gameStartManager.MainWindowHandle == IntPtr.Zero)
                {
                    Thread.Sleep(1000);
                }
                Socket.send("GameStartOK");
                while (!gameStartManager.HasExited)
                {
                    Thread.Sleep(1000);
                }
                Socket.send("GameStop");
            }).Start();
            return null;
        }
        [OriginalMethod]
        public static aov a_Original(string gjo, string gjp, EventHandler gjq, aot gjr, string gjs = null, bool gjt = false, Action<string> gju = null)
        {
            return null;
        }

        private static void CheckStart_FILE()
        {
            while (!File.Exists(FilePath))
            {
                Thread.Sleep(1000);
            }
            File.Delete(FilePath);
        }
        private static string FilePath = "C:\\Start.Game";
        public static aov gameStartManager = null;
    }

}
